package com.cynteka.cyfoman.core.config;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.List;

/**
 * User: ukman
 * Date: 20.07.2011
 * Time: 14:41:51
 */
@XmlRootElement
public class QueryMD {
    private String icon;
    private String name;
    private String sql;
    private List<ColumnMD> columns;
    private String entityName;

    public QueryMD() {
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        QueryMD queryMD = (QueryMD) o;

        if (columns != null ? !columns.equals(queryMD.columns) : queryMD.columns != null) return false;
        if (entityName != null ? !entityName.equals(queryMD.entityName) : queryMD.entityName != null) return false;
        if (icon != null ? !icon.equals(queryMD.icon) : queryMD.icon != null) return false;
        if (name != null ? !name.equals(queryMD.name) : queryMD.name != null) return false;
        if (sql != null ? !sql.equals(queryMD.sql) : queryMD.sql != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = icon != null ? icon.hashCode() : 0;
        result = 31 * result + (name != null ? name.hashCode() : 0);
        result = 31 * result + (sql != null ? sql.hashCode() : 0);
        result = 31 * result + (columns != null ? columns.hashCode() : 0);
        result = 31 * result + (entityName != null ? entityName.hashCode() : 0);
        return result;
    }

    public QueryMD(String name, String sql) {
        this.name = name;
        this.sql = sql;
    }

    public QueryMD(String name, String sql, List<ColumnMD> columns) {
        this.name = name;
        this.sql = sql;
        this.columns = columns;
    }

    @XmlElement
    public String getSql() {
        return sql;
    }

    public void setSql(String sql) {
        this.sql = sql;
    }

    @XmlElement
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public ColumnMD getColumn(String name) {
        if (columns != null) {
            for (ColumnMD columnMD : columns) {
                if (name.equalsIgnoreCase(columnMD.getName())) {
                    return columnMD;
                }
            }
        }
        return null;
    }

    @XmlElement
    public List<ColumnMD> getColumns() {
        return columns;
    }

    public void setColumns(List<ColumnMD> columns) {
        this.columns = columns;
    }

    @XmlElement
    public String getIcon() {
        return icon;
    }

    public void setIcon(String icon) {
        this.icon = icon;
    }

    @Override
    public String toString() {
        return "QueryMD{" +
                "icon='" + icon + '\'' +
                ", name='" + name + '\'' +
                ", sql='" + sql + '\'' +
                ", columns=" + columns +
                ", entityName='" + entityName + '\'' +
                '}';
    }

    @XmlElement
    public String getEntityName() {

        return entityName;
    }

    public void setEntityName(String entityName) {
        this.entityName = entityName;
    }
}
